﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using LTP.Common;

namespace DemoSwfUpload
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    public class upload : IHttpHandler
    {
        PDFConverter pdfConverter = new PDFConverter();
        SWFConverter swfConverter = new SWFConverter();
        object obj = new object();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            {
                string uploadSavePath = context.Server.MapPath("../") +
                    ConfigHelper.GetConfigString("DownloadPath").Replace("~/", "").Replace("/", "\\") +
                    DateTime.Now.Year + "_" + DateTime.Now.Month;
                string returnFilePath = ConfigHelper.GetConfigString("DownloadPath") + DateTime.Now.Year + "_" + DateTime.Now.Month;
                if (!System.IO.Directory.Exists(uploadSavePath))
                {
                    System.IO.Directory.CreateDirectory(uploadSavePath);
                }

                HttpPostedFile file;
                for (int i = 0; i < context.Request.Files.Count; ++i)
                {
                    file = context.Request.Files[i];
                    Random ra = new Random();
                    string filetp = Path.GetExtension(file.FileName);
                    string NewfileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") +
                        ra.Next(10, 99).ToString() + filetp.ToString(); //上传后文件名称！ 
                    returnFilePath = returnFilePath + "/" + NewfileName;
                    NewfileName = uploadSavePath + "\\" + NewfileName;
                    if (file == null || file.ContentLength == 0 || string.IsNullOrEmpty(file.FileName)) continue;
                    file.SaveAs(NewfileName);

                    string pdfFile = NewfileName.Substring(0, NewfileName.LastIndexOf('.')) + "_PDF.pdf";
                    string swfFile = NewfileName.Substring(0, NewfileName.LastIndexOf('.')) + "_SWF.swf";
                    System.Threading.ThreadPool.QueueUserWorkItem(delegate
                    {
                        lock (obj)
                        {
                            if (File.Exists(pdfFile))
                                File.Delete(pdfFile);
                            if (File.Exists(swfFile))
                                File.Delete(swfFile);
                            pdfConverter.Convert(NewfileName, pdfFile);
                            if (string.IsNullOrEmpty(pdfConverter.errormessage))
                            {
                                if (File.Exists(pdfFile))
                                {
                                    swfConverter.Convert(pdfFile, swfFile);
                                }
                            }
                        }
                    });
                    //context.Response.Redirect("/FlexPlayer/FlexPaperViewer.html?SwfFile=" + 
                    //    swfFile.Substring(swfFile.IndexOf("Resource"), swfFile.Length - swfFile.IndexOf("Resource")), false);
                    context.Response.Write("Success#" + returnFilePath);
                    context.ApplicationInstance.CompleteRequest();
                }
            }
            catch (Exception ex)
            {
                context.Response.StatusCode = 500;
                context.Response.Write(ex.Message);
                context.ApplicationInstance.CompleteRequest();
            }
            finally
            {
                context.ApplicationInstance.CompleteRequest();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
